package com.example.ui.leftmenu;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.opengl.Visibility;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.example.DBHelper.Dao;
import com.example.downloadmanager.Downloader;
import com.example.huashangapp.BaseView;
import com.example.huashangapp.HSApplication;
import com.example.huashangapp.R;
import com.example.service.MainService;
import com.example.ui.learn.DownHandler;
import com.example.ui.learn.DownListAdapter;
import com.example.ui.learn.DownUtils;
import com.example.ui.learn.ListAdapter;
import com.example.ui.learn.ListeningActivity;
import com.example.ui.learn.ListeningInfo;
public class Itme10View extends BaseView implements CompoundButton.OnCheckedChangeListener {
	
	private Context context;
	private Button titleLeftBtn,titleRightBtn;
	private TextView titleText;
	private Button downloaded;
	private Button downloading;
	public static ListView down_list;
	public static ListView downed_list;
	private ImageView ra_img1, ra_img2;
	public int statue=1;
	private HSApplication hsApplication;
	private ToggleButton rightBtn;
	public static SharedPreferences preferences;
	public static SharedPreferences.Editor editor;
	Set<ListeningInfo> listens;
	DownItem di;
	private Dao dao;
	public Itme10View() {
	
	}

	public Itme10View(Context context,HSApplication hsApplication) {
		this.context = context;
		this.hsApplication=hsApplication;
		dao=HSApplication.getDao();
		
	}
	public void init(){
		view = LayoutInflater.from(context).inflate(R.layout.item10, null);
		Log.i("msg", "doing Item10View");
		findView();
		DownUtils.adapter = new ListAdapter(this.context,DownUtils.downUrl);
		DownUtils.Downadapter=new DownListAdapter(context, DownUtils.listUrl);
		Log.i("down", "测试批量下载的数量"+DownUtils.downUrl.size());
		if(DownUtils.listDownItem.size()<=0){
			List<ListeningInfo> listens=dao.getListenDown();
			List<ListeningInfo> listens2=dao.getInfos();
			if(listens2.size()>0){
				for (ListeningInfo listenInfo :listens2) {
					di=new DownItem(context);
					di.setTitle(listenInfo.getListen_title());
					di.setName(listenInfo.getListen_name());  
					di.setSub_title(listenInfo.getListen_id());
					di.setUrl(listenInfo.getListen_uri());
					di.setPic_Url(listenInfo.getListen_thumb());
					di.setTer_Id(listenInfo.getListen_blId());   
					di.setColum_Id(listenInfo.getListen_itemId());
					di.setTag(listenInfo.getListen_tag());
					di.setDownLoadFileSize(listenInfo.getSize());
					di.setFileSize(listenInfo.getFileSize());
					String b=new  DecimalFormat("##.#").format(di.getFileSize()/1024/1024);
					di.getBai().setText((int)(listenInfo.getSize()*100.0f/listenInfo.getFileSize())+"%");
					di.getJindu().setText(b+"M");
					DownUtils.listDownItem.put(listenInfo.getListen_uri(),di);
					DownUtils.downUrl.add(listenInfo.getListen_uri());
					Downloader downloader = new Downloader(listenInfo.getListen_uri(), ListeningActivity.filepath,1,this.context,new DownHandler());
					DownUtils.downloaders.put(listenInfo.getListen_uri(), downloader);
					Log.i("listenInfo","文件大小"+listenInfo.getFileSize()+"下载了"+listenInfo.getSize());
					}
				}
			if(listens.size()>0){
				for (ListeningInfo listenInfo :listens) {
					di=new DownItem(context);
					di.setTitle(listenInfo.getListen_title());
					di.setName(listenInfo.getListen_name());  
					di.setSub_title(listenInfo.getListen_id());
					di.setUrl(listenInfo.getListen_uri());
					di.setPic_Url(listenInfo.getListen_thumb());
					di.setTer_Id(listenInfo.getListen_blId());   
					di.setColum_Id(listenInfo.getListen_itemId());
					di.setTag(listenInfo.getListen_tag());
					di.getPd_progressBar().setProgress(listenInfo.getSize());
					DownUtils.listDownItem.put(listenInfo.getListen_uri(),di);
					DownUtils.downUrl.add(listenInfo.getListen_uri());
					Downloader downloader = new Downloader(listenInfo.getListen_uri(), ListeningActivity.filepath,1,this.context,new DownHandler());
					DownUtils.downloaders.put(listenInfo.getListen_uri(), downloader);
					}
				}
			}
		down_list.setAdapter(DownUtils.adapter);
		down_list.setFocusable(false);
		rightBtn.setOnCheckedChangeListener(this);
		DownUtils.adapter.notifyDataSetChanged();
	}
	
	private void findView() {
		down_list=(ListView)view.findViewById(R.id.item10_ListView);
		//downed_list=(ListView)view.findViewById(R.id.item10_downed);
		titleLeftBtn = (Button)view.findViewById(R.id.layout_title_left_btn);
		titleRightBtn= (Button) view.findViewById(R.id.layout_title_right_btn);
		rightBtn = (ToggleButton)view.findViewById(R.id.layout_title_right_togglebtn);
		titleRightBtn.setVisibility(View.GONE);
		rightBtn.setVisibility(View.VISIBLE);
		titleText=(TextView)view.findViewById(R.id.layout_title_middle_text);
		titleLeftBtn.setText("菜单");
		titleText.setText("下载中心");
		
		downloaded=(Button)view.findViewById(R.id.ar_downloaded);
		downloaded.setOnClickListener(downOnClick);
		downloading=(Button)view.findViewById(R.id.ar_downloading);
		downloading.setOnClickListener(downOnClick);
		downloaded.setBackgroundResource(R.color.transparent);
		titleLeftBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				myMoveView.showHideLeftMenu();
				if(MainService.isLeftMenu==true){
					
				}else{
				
				}
			}
		});
	}
	@SuppressWarnings("unused")
	private OnClickListener downOnClick=new OnClickListener() {
		@SuppressLint("ShowToast")
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.ar_downloaded:
				if(DownUtils.listDowned.size()<=0){
					List<ListeningInfo> listens=dao.getListen();
					if(listens.size()>0){
						for (ListeningInfo listenInfo :listens) {
							Toast.makeText(context, "查询数据库"+listens.size(),Toast.LENGTH_SHORT);
							di=new DownItem(context);
							di.setTitle(listenInfo.getListen_title());
							di.setName(listenInfo.getListen_name());  
							di.setSub_title(listenInfo.getListen_id());
							di.setUrl(listenInfo.getListen_uri());
							di.setPic_Url(listenInfo.getListen_thumb());
							di.setTer_Id(listenInfo.getListen_blId());   
							di.setColum_Id(listenInfo.getListen_itemId());
							di.setTag(listenInfo.getListen_tag());
							di.setFileSize(listenInfo.getFileSize());
							DownUtils.listDowned.put(listenInfo.getListen_uri(),di);
							DownUtils.listUrl.add(listenInfo.getListen_uri());
						}
					}
				}
				if(statue==1){
					for (DownItem downitem : DownUtils.listDownItem.values()) {
							downitem.isChange=false;
							}
					}
					if(statue==2){
					for (DownItem downitem : DownUtils.listDowned.values()) {
							downitem.isChange=false;
						    }
					}
					rightBtn.setChecked(false);
					
				downloading.setBackgroundResource(R.color.transparent);
				downloaded.setBackgroundResource(R.drawable.down_select);
				Itme10View.down_list.setAdapter(DownUtils.Downadapter);
				DownUtils.Downadapter.notifyDataSetChanged();
				statue=2;
				down_list.setFocusable(false);
				break;
			case R.id.ar_downloading:
				if(statue==1){
					for (DownItem downitem : DownUtils.listDownItem.values()) {
							downitem.isChange=false;
							}
					DownUtils.adapter.notifyDataSetChanged();
					}
					if(statue==2){
					for (DownItem downitem : DownUtils.listDowned.values()) {
							downitem.isChange=false;
						    }
					DownUtils.Downadapter.notifyDataSetChanged();
					}
					rightBtn.setChecked(false);
				downloaded.setBackgroundResource(R.color.transparent);
				downloading.setBackgroundResource(R.drawable.down_select);
				//DownUtils.adatper=new ListAdapter(context, DownUtils.downUrl);
				Itme10View.down_list.setAdapter(DownUtils.adapter);
				statue=1;
				down_list.setFocusable(false);
				break;
			default:
				break;
			}
		}
	};
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if(isChecked){//编辑
			if(statue==1){
			for (DownItem downitem : DownUtils.listDownItem.values()) {
					downitem.isChange=true;
					}
			Itme10View.down_list.setAdapter(DownUtils.adapter);
			DownUtils.adapter.notifyDataSetChanged();
			}
			if(statue==2){
			for (DownItem downitem : DownUtils.listDowned.values()) {
					downitem.isChange=true;
				    }
			Itme10View.down_list.setAdapter(DownUtils.Downadapter);
			DownUtils.Downadapter.notifyDataSetChanged();
			}
		}else{
			if(statue==1){
				for (DownItem downitem : DownUtils.listDownItem.values()) {
						downitem.isChange=false;
						}
				Itme10View.down_list.setAdapter(DownUtils.adapter);
				DownUtils.adapter.notifyDataSetChanged();
				}
				if(statue==2){
				for (DownItem downitem : DownUtils.listDowned.values()) {
						downitem.isChange=false;
					    }
				Itme10View.down_list.setAdapter(DownUtils.Downadapter);
				DownUtils.Downadapter.notifyDataSetChanged();
			
				}
		}
	}
}